package combine;

import pishen.Author;
import pishen.SimpleArticle;

import hck.Article;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.ListIterator;

public class InfoCombiner {
	private ArrayList<RankedAuthor> positiveList;
	private ArrayList<RankedAuthor> negativeList;
	private ArrayList<RankedAuthor> passerbyList;
	private ArrayList<Author> authorList;
	private HashMap<String, Author> authorMap = new HashMap<String, Author>();
	private HashMap<String, SimpleArticle> mainArticleMap;

	public static void main(String[] args) {
		String targetDir = args[0];
		InfoCombiner ic = new InfoCombiner();
		ic.readInfo(targetDir);
		System.out.println("readInfo finished");
		ic.initializeAuthorMap();
		System.out.println("initializeAuthorMap finished");
		ic.mergeLists();
		System.out.println("mergeLists finished");
		ic.sortAuthor();
		System.out.println("sortAuthor finished");
		ic.writeOutput(targetDir);
		System.out.println("writeOutput finished");
	}

	public void initializeAuthorMap(){
		ListIterator<Author> authorListIterator = authorList.listIterator();
		while(authorListIterator.hasNext()){
			Author author = authorListIterator.next();
			authorMap.put(author.getName(), author);
		}
	}
	
	public void mergeLists(){
		mergeListToAuthor(positiveList, 10000);
		mergeListToAuthor(negativeList, 10000);
		mergeListToAuthor(passerbyList, 100);
	}
	
	private void mergeListToAuthor(ArrayList<RankedAuthor> targetList, int limit){
		double totalWeight = 0;
		ListIterator<RankedAuthor> targetListIterator = targetList.listIterator();
		while(targetListIterator.hasNext()){
			RankedAuthor rankedAuthor = targetListIterator.next();
			totalWeight += rankedAuthor.getPrestige();
		}
		targetListIterator = targetList.listIterator();
		while(targetListIterator.hasNext()){
			RankedAuthor rankedAuthor = targetListIterator.next();
			double addedValue = (rankedAuthor.getPrestige() / totalWeight) * limit;
			authorMap.get(rankedAuthor.getName()).addPrestige(addedValue);
		}
	}
	
	public void sortAuthor(){
		Collections.sort(authorList);
	}
	
	public void writeOutput(String targetDir){
		ListIterator<Author> authorListIterator = authorList.listIterator();
		BufferedWriter bw;
		try {
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetDir + "finalrank")));
			while(authorListIterator.hasNext()){
				Author author = authorListIterator.next();
				bw.write(author.getName() + "  " + author.getPrestige());
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void readInfo(String targetDir){
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(targetDir + "positive_graph_result.ser"));
			positiveList = (ArrayList<RankedAuthor>) in.readObject();
			in.close();
			in = new ObjectInputStream(new FileInputStream(targetDir + "negative_graph_result.ser"));
			negativeList = (ArrayList<RankedAuthor>) in.readObject();
			in.close();
			in = new ObjectInputStream(new FileInputStream(targetDir + "passerby_result.ser"));
			passerbyList = (ArrayList<RankedAuthor>) in.readObject();
			in.close();
			in = new ObjectInputStream(new FileInputStream(targetDir + "author_collection.ser"));
			authorList = (ArrayList<Author>) in.readObject();
			//authorMap = (HashMap<String, Author>) in.readObject();
			mainArticleMap = (HashMap<String, SimpleArticle>) in.readObject();
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
